<template>
  <div class="form-layout">
   <!--左侧导航-->
   <div class="nav-left">
     <customAnchor
       :itemList="[{name: 'box-base',title: '基础信息'}, {name: 'box-more',title: '效期信息'}]"
     />
   </div>

   <!--表单区域-->
   <div  class="form-right" ref="formRight">
     <customForm
       :data="form"
       :rules="propRules"
       ref="editForm"
       :disabled="propReadonly"
     >
       <template #content>
         <div id="box-base" class="box-title">基础信息</div>
         <customFormItem
           v-show="form.targetFlag === 1"
           label="厂商名称"
           name="targetName"
           :parent="_self"
           :form="form"
           :readonly="true"
           :showSelectIcon="true"
           @focus="$refs.tableDrawerBase.show()"
         />
         <customFormItem
           v-show="form.targetFlag !== 1"
           label="供应商名称"
           name="targetName"
           :parent="_self"
           :form="form"
           :readonly="true"
           :showSelectIcon="true"
           @focus="$refs.tableDrawerSupplier.show()"
         />
         <customFormItem
           label="资质名称"
           name="certificateName"
           :parent="_self"
           :form="form"
         />
         <customFormItem
           label="资质编号"
           name="certificateCode"
           :parent="_self"
           :form="form"
         />
         <customFormItem
           label="状态"
           name="closeFlag"
           :parent="_self"
           :form="form"
           :type="constCommon.formItemType.selectStatic.name"
           :config="{options: closeFlags}"
           help="状态为关闭时，表示该资质失效，但失效之前的所有属性不影响，关闭后不在进行对应耗材的效期判断"
         />
         <customFormItem
           label="资质归属"
           name="targetFlag"
           :parent="_self"
           :form="form"
           :type="constCommon.formItemType.selectStatic.name"
           :config="{options: targetFlagOptions}"
           :disabled="form.closeFlag == 0 && strings.isNotBlank(form.id)"
           @change="targetFlagChange"
         />
         <customFormItem
           label="资质类型"
           name="certificateType"
           :parent="_self"
           :form="form"
           :type="constCommon.formItemType.selectStatic.name"
           :config="{options: form.targetFlag == 0 ? certificateTypeOptions_surpply : certificateTypeOptions_mfg}"
           :disabled="form.closeFlag == 0 && strings.isNotBlank(form.id)"
         />
         <customFormItem
            label="备注"
            name="remark"
            :parent="_self"
            :form="form"
          />

         <div id="box-more" class="box-title">效期信息</div>
         <customFormItem
           label="开始日期"
           name="expiryStartDate"
           :parent="_self"
           :form="form"
           :type="constCommon.formItemType.datePicker.name"
         />
         <customFormItem
           label="截止日期"
           name="expiryDate"
           :parent="_self"
           :form="form"
           :type="constCommon.formItemType.datePicker.name"
         />
         <customFormItem
           label="预警天数"
           name="earlyWarningDays"
           :parent="_self"
           :form="form"
           :type="constCommon.formItemType.inputNumber.name"
           @change="earlyWarningDaysChange"
         />
       </template>
     </customForm>
   </div>
  <!--厂商名称选择-->
  <tableDrawer ref="tableDrawerBase" refName="tableDrawerBase" @confirm="tableDrawerConfirm" viewKey="base_mfg"/>
  <!--供应商名称选择-->
  <tableDrawer ref="tableDrawerSupplier" refName="tableDrawerSupplier" @confirm="tableDrawerConfirm" viewKey="select_supplier"/>
 </div>
</template>

<script>
import customAnchor from '@/components/custom/form/customAnchor.vue'

export default {
  data() {
    return {
      //资质归属标志 0-供应商 1-厂商
      targetFlagOptions: [{
        value: 0,
        label: '供应商'
      }, {
        value: 1,
        label: '厂商'
      }],

      certificateTypeOptions: [
        //供应商经营许可证、供应商营业执照、供应商授权书、厂商营业执照、注册证、厂商生产许可证、厂商授权书
        {
          value: 1,
          label: '供应商经营许可证'
        },
        {
          value: 2,
          label: '供应商营业执照'
        },
        {
          value: 3,
          label: '供应商授权书'
        }
      ],
      certificateTypeOptions_surpply: [
        //供应商经营许可证、供应商营业执照、供应商授权书、厂商营业执照、注册证、厂商生产许可证、厂商授权书
        {
          value: 1,
          label: '供应商经营许可证'
        },
        {
          value: 2,
          label: '供应商营业执照'
        },
        {
          value: 3,
          label: '供应商授权书'
        }
      ],
      certificateTypeOptions_mfg: [
        //供应商经营许可证、供应商营业执照、供应商授权书、厂商营业执照、注册证、厂商生产许可证、厂商授权书
        {
          value: 4,
          label: '厂商经营许可证'
        },
        {
          value: 5,
          label: '厂商营业执照'
        },
        {
          value: 6,
          label: '厂商授权书'
        }
      ],
      closeFlags: [
        //关闭状态。0：正常；1：已关闭（作废）
        {
          value: false,
          label: '正常'
        },
        {
          value: true,
          label: '已关闭（作废）'
        }
      ],

      form: {},
      viewData: {}
    }
  },
  created() {
    this.$emit('event-register', {
      validate: (callback) => {
        return this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)
      },
      afterSave: () => {
        // 用作返回自动主键
        this.form[this.propPrimaryKey] = this.propViewFormData[this.propPrimaryKey]
      },
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {
      },
      beforeFlow: (flowButtonKey) => {
        return true
      },
      afterFlow: (flowButtonKey) => {
      },
      beforeClose: () => {
        return true
      }
    })
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object
  },
  components: {
    customAnchor
  },
  computed: {
  },
  watch: {
    /*if (this.form.targetFlag === 1){

    }*/
  },
  methods: {
    earlyWarningDaysChange() {
      if (!this.form.earlyWarningDays || !(/(^[1-9]\d*$)/.test(this.form.earlyWarningDays)) || this.form.earlyWarningDays <= 0) {
        global.utilMessage.alertFail('预警天数必须为正数！')
        this.$set(this.form, 'earlyWarningDays', '')
        return false
      }
      return true
    },
    targetFlagChange() {
      this.$set(this.form, 'certificateType', '请选择')
    },
    //选择供应商事件
    selectSupplier({fieldText, row}) {
      this.$set(this.form, 'targetId', row.id)
      this.$set(this.form, 'targetName', row.name)
    },
    getChildViewQueryWrapper() {
      if (this.form.id) {
        return this.$newWrapper().eq('orderId', this.form.id)
      } else {
        return this.$newWrapper().ne('1', 1)
      }
    },
    getChildRows() {
      if (!this.$refs.childDataView) {
        return []
      }

      //供应商、订单类型、一物一码类型改变后都需要更换耗材查询参数
      this.syncMatr()
      return this.$refs.childDataView.getRows()
    },
    changePy() {
      this.form.namePy = this.pyutils.getAllFirstLetter(this.form.name)
    },
    // 选择
    tableDrawerConfirm(row) {
      console.log('row', row)
      this.$set(this.form, 'targetId', row.id)
      this.$set(this.form, 'targetName', row.name)
    }
  }
}
</script>
